package com.servlet;

import com.Dao.Info.Student;
import com.Dao.StudentDao;
import com.util.JsonUtils;
import com.util.ListObject;
import com.util.ResponseUtils;
import com.util.StatusCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:xuyuanhong
 * @Date:Created in 2022/12/10 14:26
 */
@WebServlet("/ScoreInfo")
public class ScoreInfo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentDao studentDao=new StudentDao();
        List<Student> StudentList = null;
        try {
            StudentList = studentDao.querytwo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 添加数据
        List<HashMap> list = new ArrayList<HashMap>();
        for (int i = 0; i < StudentList.size() ; i++) {
            HashMap map = new HashMap();
            map.put("name",StudentList.get(i).getTerms());
            map.put("num",StudentList.get(i).getNum());
            list.add(map);
        }
        // 添加数据
//        List<HashMap> list = new ArrayList<HashMap>();
//        for (int i = 0; i < StudentList.size() ; i++) {
//            HashMap map = new HashMap();
//            map.put("id",StudentList.get(i).getSid());
//            map.put("score",StudentList.get(i).getScore());
//            list.add(map);
//        }
        // 封装    json
        ListObject listObject = new ListObject();
        listObject.setItems(list);
        listObject.setCode(StatusCode.CODE_SUCCESS);
        listObject.setMsg("成功");

        try {
            ResponseUtils.renderJson(resp, JsonUtils.toJson(listObject));

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
